

#include <QtGui>

#include "highlighter.h"

//! [0]
Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    HighlightingRule rule;

    keywordFormat.setForeground(Qt::black);
    keywordFormat.setFontWeight(QFont::Bold);
    QStringList keywordPatterns;
//    keywordPatterns << "\\bchar\\b" << "\\bvar\\b" << "\\bconst\\b"
//            << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
//            << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
//            << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
//            << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
//            << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
//            << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
//            << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
//            << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
//            << "\\bvoid\\b" << "\\bvolatile\\b";


    QFile file(":/resources/wordlist.txt");
    if (file.open(QFile::ReadOnly)){
        while (!file.atEnd()) {
            QByteArray line = file.readLine();
            if (!line.trimmed().isEmpty())
                keywordPatterns << "\\b" + line.trimmed() + "\\b";
        }
    }


    foreach (const QString &pattern, keywordPatterns) {
        rule.pattern = QRegExp(pattern);
        rule.format = keywordFormat;
        highlightingRules.append(rule);
        //! [0] //! [1]
    }
    //! [1]

    //! [2]
    classFormat.setFontWeight(QFont::Bold);
    classFormat.setForeground(Qt::blue);
    rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    rule.format = classFormat;
    highlightingRules.append(rule);
    //! [2]

    //! [3]
    singleLineCommentFormat.setForeground(Qt::green);
    rule.pattern = QRegExp("//[^\n]*");
    rule.format = singleLineCommentFormat;
    highlightingRules.append(rule);

    multiLineCommentFormat.setForeground(Qt::green);
    //! [3]

    //! [4]
    quotationFormat.setForeground(Qt::gray);
    quotationFormat.setFontItalic(true);
    rule.pattern = QRegExp("\".*\"");
    rule.format = quotationFormat;
    highlightingRules.append(rule);
    //! [4]

    //! [5]    
    functionFormat.setForeground(Qt::blue);
    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
    rule.format = functionFormat;
    highlightingRules.append(rule);
    //! [5]

    //! [6]
    commentStartExpression = QRegExp("/\\*");
    commentEndExpression = QRegExp("\\*/");
}
//! [6]

//! [7]
void Highlighter::highlightBlock(const QString &text)
{
    foreach (const HighlightingRule &rule, highlightingRules) {
        QRegExp expression(rule.pattern);
        int index = expression.indexIn(text);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = expression.indexIn(text, index + length);
        }
    }
    //! [7] //! [8]
    setCurrentBlockState(0);
    //! [8]

    //! [9]
    int startIndex = 0;
    if (previousBlockState() != 1)
        startIndex = commentStartExpression.indexIn(text);

    //! [9] //! [10]
    while (startIndex >= 0) {
        //! [10] //! [11]
        int endIndex = commentEndExpression.indexIn(text, startIndex);
        int commentLength;
        if (endIndex == -1) {
            setCurrentBlockState(1);
            commentLength = text.length() - startIndex;
        } else {
            commentLength = endIndex - startIndex
                            + commentEndExpression.matchedLength();
        }
        setFormat(startIndex, commentLength, multiLineCommentFormat);
        startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
    }
}
//! [11]
